# -*- coding: utf-8 -*-

'''eof
name:申请企业强制执行记录校验
code:R54_EG_CASE_EntEnforcementRecordCheck_XD
tableName:
columnName:
groups:个人及企业司法校验
dependencies:EG_CASE_DSJ
type:常用指标
datasourceType:在线指标
description:
eof'''

import sys
reload(sys)
sys.setdefaultencoding('utf-8')
from datetime import datetime


def FormatData(tableName):
    try:
        eg = EG_CASE_DSJ.get("data").get(tableName)
        return eg if isinstance(eg, list) else [{}]
    except:
        return [{}]


# 统一转成datetime对象，只能转年月日的字符串
def date_format(time):
    try:
        import re
        import datetime
        r1 = re.compile(u'年|月|日')
        time = r1.sub('.', time)
        try:
            date = datetime.datetime.strptime(time, '%Y.%m.%d')
        except:
            try:
                date = datetime.datetime.strptime(time, '%Y-%m-%d')
            except:
                try:
                    date = datetime.datetime.strptime(time, '%Y/%m/%d')
                except:
                    try:
                        r = re.compile(r'\D+')
                        tmp = r.sub('', time)
                        if len(tmp) > 8:
                            tmp = tmp[:8]
                        date = datetime.datetime.strptime(tmp, '%Y%m%d')
                    except:
                        date = None
        return date
    except:
        return None


# 获取 上次申请时间
def get_relative_date():
    relative_date = ratingRequest.get('relativeDate')
    return date_format(relative_date)


def diff_date(str):
    try:
        date1 = datetime.now()
        date2 = date_format(str[0:10])
        diff_year = date1.year - date2.year
        diff_month = date1.month - date2.month
        diff_day = date1.day - date2.day
        year = diff_year + diff_month / 12.0 + diff_day / 365.0
        return year
    except:
        return None


def float_num(str):
    try:
        return float(str)
    except:
        return None


null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']


def getRule54():
    zhixingList_oldname = FormatData("FR228")
    zhixingList_nowname = FormatData("R228")
    if zhixingList_nowname == [{}] and zhixingList_oldname == [{}]:
        return u"缺失值"
    elif zhixingList_nowname == [] and zhixingList_oldname == []:
		return 1
    else:
        duplicate_list = []
        resultList = []
        relativeDate = get_relative_date()
        if relativeDate is None:
            raise ValueError(u"网贷传输的数据字段relativeDate缺失或不符合要求")
        if zhixingList_nowname != [{}]:
            for i in zhixingList_nowname:
                EXECMONEY = float_num(i.get("EXECMONEY"))
                CASECODE = i.get("CASECODE")
                CASECREATETIME = i.get("CASECREATETIME")
                if CASECREATETIME not in null_type_list and diff_date(CASECREATETIME) !=None and diff_date(CASECREATETIME) > 5:
                    resultList.append(1)
                elif EXECMONEY not in null_type_list and CASECODE not in null_type_list and \
                        date_format(CASECREATETIME) not in null_type_list and \
                        (CASECREATETIME + CASECODE) not in duplicate_list and EXECMONEY > 0 and diff_date(CASECREATETIME) !=None:
                    ## 将立案时间限定在 上一次申请时间之间
                    if date_format(CASECREATETIME) > relativeDate and diff_date(CASECREATETIME) !=None:
                        duplicate_list.append(CASECREATETIME + CASECODE)
                        if date_format(CASECREATETIME) in null_type_list:
                            resultList.append(2)
                        elif diff_date(CASECREATETIME) < 0 and diff_date(CASECREATETIME) !=None:
                            resultList.append(2)
                        elif diff_date(CASECREATETIME) <= 2 and diff_date(CASECREATETIME) !=None:
                            if EXECMONEY < 6000:
                                resultList.append(1)
                            else:
                                resultList.append(0)
                        else:
                            resultList.append(1)
                    else:
                        resultList.append(1)
                elif EXECMONEY in null_type_list or EXECMONEY <= 0 \
                        or CASECODE in null_type_list or CASECREATETIME in null_type_list or diff_date(CASECREATETIME)==None:
                    resultList.append(2)
        else:
            pass

        if zhixingList_oldname != [{}]:
            for i in zhixingList_oldname:
                EXECMONEY = float_num(i.get("EXECMONEY"))
                CASECODE = i.get("CASECODE")
                CASECREATETIME = i.get("CASECREATETIME")
                if CASECREATETIME not in null_type_list and diff_date(CASECREATETIME) != None and diff_date(
                        CASECREATETIME) > 5:
                    resultList.append(1)
                elif EXECMONEY not in null_type_list and CASECODE not in null_type_list and \
                        date_format(CASECREATETIME) not in null_type_list and \
                        (CASECREATETIME + CASECODE) not in duplicate_list and EXECMONEY > 0 and diff_date(
                    CASECREATETIME) != None:
                    ## 将立案时间限定在 上一次申请时间之间
                    if date_format(CASECREATETIME) > relativeDate and diff_date(CASECREATETIME) != None:
                        duplicate_list.append(CASECREATETIME + CASECODE)
                        if date_format(CASECREATETIME) in null_type_list:
                            resultList.append(2)
                        elif diff_date(CASECREATETIME) < 0 and diff_date(CASECREATETIME) != None:
                            resultList.append(2)
                        elif diff_date(CASECREATETIME) <= 2 and diff_date(CASECREATETIME) != None:
                            if EXECMONEY < 6000:
                                resultList.append(1)
                            else:
                                resultList.append(0)
                        else:
                            resultList.append(1)
                    else:
                        resultList.append(1)
                elif EXECMONEY in null_type_list or EXECMONEY <= 0 \
                        or CASECODE in null_type_list or date_format(CASECREATETIME) in null_type_list or diff_date(
                    CASECREATETIME) == None:
                    resultList.append(2)
        else:
            pass

        if resultList == []:
            return 1
        elif min(resultList) == 0:
            return 0
        elif max(resultList) == 2:
            return 2
        else:
            return 1

result = getRule54()

